<?php

namespace App\Console\Commands\OneDragon;

use Illuminate\Console\Command;
use Illuminate\Support\Str;
use Nwidart\Modules\Support\Config\GenerateConfigReader;
use Nwidart\Modules\Traits\ModuleCommandTrait;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Input\InputArgument;

class OneDragonMakeController extends GeneratorCommand
{
    use ModuleCommandTrait;

    /**
     * The name of argument being used.
     *
     * @var string
     */
    protected $argumentName = 'controller';

    /**
     * The console command name.
     *
     * @var string
     */
    protected $name = 'module:make-one-dragon-controller';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Create a new one-dragon-controller.';

    /**
     * Get controller name.
     *
     * @return string
     */
    public function getDestinationFilePath()
    {
        $path = $this->laravel['modules']->getModulePath($this->getModuleName());

        $controllerPath = GenerateConfigReader::read('controller');

        return $path . $controllerPath->getPath() . '/' . $this->getControllerName() . '.php';
    }

    /**
     * @return string
     */
    protected function getTemplateContents()
    {
        $module = $this->laravel['modules']->findOrFail($this->getModuleName());

        // 验证器
        $OneDragonMakeRequest = new OneDragonMakeRequest;
        $request_name = $OneDragonMakeRequest->getFileName($this->argument('controller')) . 'Request';
        // 服务层
        $OneDragonMakeService = new OneDragonMakeService;
        $service_name = $OneDragonMakeService->getFileName($this->argument('controller')) . 'Service';

        $stub = new Stub($this->getStubName(), [
            'MODULENAME'        => $module->getStudlyName(),
            'CONTROLLERNAME'    => $this->getControllerName(),
            'NAMESPACE'         => $module->getStudlyName(),
            'CLASS_NAMESPACE'   => $this->getClassNamespace($module),
            'CLASS'             => $this->getControllerNameWithoutNamespace(),
            'LOWER_NAME'        => $module->getLowerName(),
            'MODULE'            => $this->getModuleName(),
            'NAME'              => $this->getModuleName(),
            'STUDLY_NAME'       => $module->getStudlyName(),
            'MODULE_NAMESPACE'  => $this->laravel['modules']->config('namespace'),

            'REQUEST_NAMESPACE' => str_replace('Controllers', 'Requests', $this->getClassNamespace($module))  . '\\' . $request_name,
            'REQUEST_NAME' => $request_name,

            'SERVICE_NAMESPACE' => str_replace('Http\\Controllers', 'Services', $this->getClassNamespace($module))  . '\\' . $service_name,
            'SERVICE_NAME' => $service_name,
        ]);

        return $stub->render();
    }

    /**
     * Get the console command arguments.
     *
     * @return array
     */
    protected function getArguments()
    {
        return [
            ['controller', InputArgument::REQUIRED, 'The name of the controller class.'],
            ['module', InputArgument::OPTIONAL, 'The name of module will be used.'],
        ];
    }

    /**
     * @return array|string
     */
    protected function getControllerName()
    {
        $controller = Str::studly($this->argument('controller'));

        if (Str::contains(strtolower($controller), 'controller') === false) {
            $controller .= 'Controller';
        }

        return $controller;
    }

    /**
     * @return array|string
     */
    private function getControllerNameWithoutNamespace()
    {
        return class_basename($this->getControllerName());
    }

    public function getDefaultNamespace($rootNamespace) : string
    {
        parent::getDefaultNamespace($rootNamespace); // TODO: Change the autogenerated stub

        $module = $this->laravel['modules'];

        return $module->config('paths.generator.controller.namespace') ?: $module->config('paths.generator.controller.path', 'Http/Controllers');
    }

    /**
     * Get the stub file name based on the options
     * @return string
     */
    protected function getStubName()
    {
        $stub = '/controller.plain.stub';

        return $stub;
    }
}
